package edu.gd.servlet;

import com.mysql.cj.util.StringUtils;
import edu.gd.dao.UserDao;
import edu.gd.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/user/update/")
public class UserUpdateServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String sex=req.getParameter("sex");
        String question=req.getParameter("question");
        String answer=req.getParameter("answer");
        String introduce=req.getParameter("introduce");
        User u1= UserDao.getUserByID(username);
        int rows=0;
        if(u1!=null&&!StringUtils.isEmptyOrWhitespaceOnly(sex)){
            u1.setSex(sex);
            u1.setQuestion(question);
            u1.setAnswer(answer);
            u1.setIntroduce(introduce);
            rows=UserDao.updateUserByUser(u1);
        }
        resp.sendRedirect(req.getContextPath()+"/update_state.jsp?r="+rows);
    }
}
